var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

// import { $wuxRefresher } from '../../components/wux';
import { $wuxPrompt } from '../../components/wux';


var app = getApp();
var that;

let  pageInex = 1;

Page({
  data: { 
    user:null,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false
      ,inputShowed: false,
    inputVal: ""
  },
  onPullDownRefresh:function(){
    pageInex = 1;
    this.start=true;
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:false
    });
    util.showLoading();
    that.loadData();
  },
  onShow:function(){
    

    
  },
  onLoad: function (query) {
      that = this;

      that.data._sdesc="D几名";
    wx.setNavigationBarTitle( { title: that.data._sdesc });

    $wuxPrompt.init('msg1', {
      title: '暂时没有相关数据', 
    }).show()

    this.query = query;

    pageInex = 1;
    util.showLoading();
    app.getUserInfo()
    .then(res => {
        that.setData({
            user: res
        })
        that.loadData();
    });

    // that.refresher = new $wuxRefresher({
    //   onPulling() {
    //   },
    //   onRefresh() {
    //     pageInex = 1;
    //     this.start=true;
    //     that.setData({
    //       list : [],
    //       isLoadMore : false,
    //       isEnd : false,
    //       isEmpty:false
    //     });

    //     setTimeout(() => {
          
    //       that.loadData();
    //     },1000);
    //   }
    // })

  },
  // touchstart(e) {
  //   that.refresher.touchstart(e)
  // }, 
  // touchmove(e) {
  //   that.refresher.touchmove(e)
  // }, 
  // touchend(e) {
  //   that.refresher.touchend(e)
  // }, 
  onReady : function(){
    

  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onReachBottom : function(e,a){
      //加载更多
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadData();
      }
  },
  
  onForwardColumn : function(){
      wx.navigateTo({
        url : "../column/column"
      });
  },

  onPreImage : function(e){
      let src = e.currentTarget.dataset.imgSrc;
      Util.UI.preImage(src);
  },
  loadData: function(){

    that.setData({
      isLoadMore : true
    });

    var url = config.url_team_list;
    var params = {
        pageIndex: pageInex,
        pageSize: app.globalData.defaultPageSize,
        name:that.data.inputVal
        ,mobile:"10000"
    }

    wxRequest.postRequest(url, params)
    .then(res=>{
      
      // if(that.refresher.start==true){
      //   that.refresher.start=false;
      //   that.refresher.events.emit(`scroll.refreshComplete`);
      // }

      if(pageInex==1 && res.length==0){
          that.setData({
            isLoadMore : false,
            isEnd : false,
            isEmpty :true,
          });
          return;
      }
      
      if(res.length > 0){
        pageInex++;

        for(var i in res){
          var now = new Date().getTime();
          var bd = util.str2date(res[i].bdate).getTime();
          var ed = util.str2date(res[i].edate).getTime();
          var ld = util.str2date(res[i].ldate).getTime();
          //判断
          var status = 0;
          var str="未开始";
          if(now  <  bd){
            status = 0;
             str="未开始";
          }else if( now >= bd && now <= ed )
          {
            status = 1;
            str="进行中";
          }
          else if( now > ed && now <= ld )
          {
            status = 2;
            str="评奖中";
          }
          else{
            status = 3;
            str="已结束";
          }
          var isclose = res[i].isclose;
          if(isclose ==1){
              status = 3;
              str="已关闭";
          }

          res[i].str = str;
          res[i].status = status;
          res[i].bdate = util.date2str(res[i].bdate,"MM月dd日");
          res[i].edate = util.date2str(res[i].edate,"MM月dd日");
        }
        var data = that.data.list.concat(res);   
        that.setData({
            list : data,
            isLoadMore : false
        });
      }
      
      if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
        that.setData({
          isLoadMore : false,
          isEnd : true
        });
      }

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
  },
  onrow:function(e){
    var data = e.currentTarget.dataset;

    wx.navigateTo({
        url: '../user/qdlist?tid='+data.id
    })

    //util.showLoading();

    //var url = config.url_team_user_is;
    //var params = {
    //  id:data.id
    //}
    //wxRequest.postRequest(url, params)
    //.then(res => {
    //    if(util.judge.isNull(res)){
            
      
    //    }else{

    //        if(res==21){
    //            util.alert("禁止进入");
    //            throw util.stop();
    //        }else{
    //            wx.navigateTo({
    //                url: '../team/main?tid='+ data.id
    //            });
    //        }
    //    }
    //})
    //.catch(res => {
    //    util.alert_catch(res.message);
    //})
    //.finally(function (res) {
    //    util.hideLoading();
    //});

    
  },
  onedit:function(e){

      var url = config.url_user_instm;
      var params = {
          fid:e.detail.formId
      }

      wxRequest.postRequest(url, params)
      .then(res=>{
      });

      wx.navigateTo({
          url: '../team/edit?type=0&tid=0'
      })
  }
    ,oneditjs:function(e){

        var url = config.url_user_instm;
        var params = {
            fid:e.detail.formId
        }

        wxRequest.postRequest(url, params)
        .then(res=>{
        });

        wx.navigateTo({
            url: '../team/edit?type=1&tid=0'
        })
    }

    ,showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        //this.setData({
        //    inputVal: "",
        //    inputShowed: false
        //});
        setTimeout(that.onPullDownRefresh,200);
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
            ,inputShowed: false
        });
        //重新加载
        setTimeout(that.onPullDownRefresh,200);
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
    }
})


